package com.arsn.myf.system.service.impl;

import com.arsn.myf.common.node.TreeNode;
import com.arsn.myf.common.util.node.NodeTreeUtil;
import com.arsn.myf.system.entity.Role;
import com.arsn.myf.system.entity.RoleMenu;
import com.arsn.myf.system.mapper.RoleMapper;
import com.arsn.myf.system.service.RoleMenuService;
import com.arsn.myf.system.service.RoleService;
import com.arsn.myf.system.vo.RoleMenuVO;
import com.arsn.myf.system.vo.RoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色实现层
 * @author zhongrj
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 自定义查询角色分页数据
     * @param role
     * @return
     */
    @Override
    public List<RoleVO> selectPageRoleList(RoleVO role) {
        return NodeTreeUtil.getRoleNodeTree(baseMapper.selectPageRoleList(role));
    }

    /**
     * 查询角色列表
     * @param role
     * @return
     */
    @Override
    public List<RoleVO> getRoleList(RoleVO role) {
        return baseMapper.getRoleList(role);
    }

    /**
     * 获取角色树
     * @param role 角色查询参数对象
     * @return
     */
    @Override
    public List<TreeNode> getRoleTree(RoleVO role) {
        return NodeTreeUtil.getNodeTree(baseMapper.getRoleTree(role));
    }

    /**
     * 角色菜单配置
     * @param roles (roleId 角色id,menuIds 菜单id)
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public boolean updateMenuRole(RoleMenuVO roles) {
        //先查询当前角色对应的角色菜单集合
        List<Long> roleMenuList = baseMapper.getRoleMenuList(roles.getRoleId());
        List<Long> menuIds = roles.getMenuIds();
        //对比差值
        if (roleMenuList.size()==0){
            //新增
            if (menuIds.size()>0){
                for (Long id : menuIds) {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setMenuId(id);
                    roleMenu.setRoleId(Long.parseLong(roles.getRoleId()));
                    roleMenuService.save(roleMenu);
                }
            }
        }else {
            //对比差值
            List<Long> addList = menuIds.stream().filter(item -> !roleMenuList.contains(item)).collect(Collectors.toList());
            List<Long> delList = roleMenuList.stream().filter(item -> !menuIds.contains(item)).collect(Collectors.toList());
            //分别处理
            //新增
            if (addList.size()>0){
                for (Long id : addList) {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setMenuId(id);
                    roleMenu.setRoleId(Long.parseLong(roles.getRoleId()));
                    roleMenuService.save(roleMenu);
                }
            }
            //删除
            if (delList.size()>0){
                for (Long menuId : delList) {
                    QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
                    wrapper.eq("role_id",roles.getRoleId()).eq("menu_id",menuId);
                    roleMenuService.remove(wrapper);
                }
            }
        }
        return true;
    }
}
